<template>
  <div>

    <treedata :nodes="nodes.metadata"
              :item="nodes"
              :md_show2="nodes.md_show"
              :label="nodes.md_title"
              :leve=0></treedata>
  </div>
</template>
<script>
import treedata from "./treedata";
import metadatajson from "@/assets/data.json";
import EventBus from "@/utils/EventBus";
export default {
  components: {
    treedata
  },
  watch: {
    val() {
      this.handelVal()
    }
  },
  data() {
    return {
      nodes: metadatajson[0],
      item: {},
      val: ""
    }
  },
  created() {
    this.handelData(this.nodes)
  },
  mounted() {
    EventBus.$on("getCurClickItem", item => {
      this.item = item;
      this.handelMetaDataShowStatus(this.nodes)
    })
  },
  methods: {
    handelData(val) {
      function recur(val) {
        val.forEach(v => {
          if (!Array.isArray(v.metadata) && v.metadata) {
            v.metadata = new Array(v.metadata)
          }
          if (v.metadata) {
            recur(v.metadata)
          }
        })
      }
      recur(val.metadata);


    },
    handelMetaDataShowStatus(nodes) {
      let that = this;
      if (that.item.md_id == nodes.md_id) {
        nodes.md_show = !nodes.md_show
        return
      }
      function updateShowStatus(nodes) {
        nodes.forEach(ele => {
          if (ele.md_id == that.item.md_id) {
            ele.md_show = !ele.md_show
            return
          }
          if (ele.metadata) {
            updateShowStatus(ele.metadata)
          }
        });
      }
      updateShowStatus(nodes.metadata)
    }
  },
}
</script>
<style lang='scss' scoped>
</style>